package git.soulbgm.thread;

import git.soulbgm.pojo.ProduceAlarmInfo;
import git.soulbgm.service.AlarmInfoService;
import git.soulbgm.utils.LogHelper;
import git.soulbgm.websocket.AlarmWebSocketServer;

/**
 * 处理产生告警信息线程
 *
 * @author SoulBGM
 * @date 2023-04-25
 */
public class ProduceAlarmInfoThread implements Runnable {

    private static final LogHelper LOG = new LogHelper(ProduceAlarmInfoThread.class);

    private final ProduceAlarmInfo produceAlarmInfo;
    private final AlarmInfoService alarmInfoService;

    public ProduceAlarmInfoThread(ProduceAlarmInfo produceAlarmInfo, AlarmInfoService alarmInfoService) {
        this.produceAlarmInfo = produceAlarmInfo;
        this.alarmInfoService = alarmInfoService;
    }

    @Override
    public void run() {
        if (produceAlarmInfo != null) {
            alarmInfoService.produceAlarmInfoHandle(produceAlarmInfo);
        }
        try {
            AlarmWebSocketServer.broadcast(alarmInfoService.getAlarmCountVo());
        } catch (Exception e) {
            LOG.error("向前端推送告警数量时发生异常", e);
        }
    }
}
